﻿Public Class Form1

    Dim sReady, data, imgLight, buzzer, key As Boolean
    Dim Reading As String


    Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If SerialPort1.IsOpen Then
            SerialPort1.Write("S")
            SerialPort1.Close()
        End If

    End Sub

    Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = (Keys.Up) Then
            If SerialPort1.IsOpen And key Then
                SerialPort1.Write("U")
                key = False
                data = False
            End If
        ElseIf e.KeyCode.Equals(Keys.Down) Then
            If SerialPort1.IsOpen And key Then
                SerialPort1.Write("D")
                key = False
                data = False
            End If
        ElseIf e.KeyCode.Equals(Keys.Right) Then
            If SerialPort1.IsOpen And key Then
                SerialPort1.Write("R")
                key = False
                data = False
            End If
        ElseIf e.KeyCode.Equals(Keys.Left) Then
            If SerialPort1.IsOpen And key Then
                SerialPort1.Write("L")
                key = False
                data = False
            End If
        End If
    End Sub

    Private Sub Form1_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If SerialPort1.IsOpen And Not key Then
            SerialPort1.Write("S")
            key = True
            data = False
        End If
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        btnBWD.Enabled = False
        btnData.Enabled = False
        btnFWD.Enabled = False
        btnStop.Enabled = False
        btnRight.Enabled = False
        btnLeft.Enabled = False
        btnDcnct.Enabled = False
        btnSensor.Enabled = False
        btnLight.Enabled = False
        btnSpeaker.Enabled = False
        data = False
        sReady = False
        imgLight = False
        buzzer = False
        Reading = "..."
        SerialPort1.PortName = vbNull
        Me.MaximizeBox = False
        Me.KeyPreview = True
    End Sub

    Private Sub ComboBox1_Click(sender As Object, e As System.EventArgs) Handles ComboBox1.Click
        ComboBox1.Items.Clear()
        ComboBox1.Items.AddRange(IO.Ports.SerialPort.GetPortNames())
        If ComboBox1.Items.Count = 0 Then
            MsgBox("No Device is Connected", , "Attention")
        End If
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        SerialPort1.PortName = ComboBox1.SelectedItem.ToString
        sReady = True
    End Sub

    Private Sub btnCnct_Click(sender As System.Object, e As System.EventArgs) Handles btnCnct.Click

        Try
            SerialPort1.Open()
        Catch ex As Exception
            MsgBox("No COM is selected or COM selected is used", , "Attention")
        End Try

        If SerialPort1.IsOpen Then
            btnBWD.Enabled = True
            btnData.Enabled = True
            btnFWD.Enabled = True
            btnStop.Enabled = True
            btnRight.Enabled = True
            btnLeft.Enabled = True
            btnLight.Enabled = True
            btnSpeaker.Enabled = True
            btnDcnct.Enabled = Not btnDcnct.Enabled
            btnSensor.Enabled = True
            btnCnct.Enabled = False
            key = True
            ComboBox1.Enabled = False
        End If
    End Sub

    Private Sub btnDcnct_Click(sender As System.Object, e As System.EventArgs) Handles btnDcnct.Click
        SerialPort1.Write("S")
        SerialPort1.Close()
        If Not SerialPort1.IsOpen Then
            btnBWD.Enabled = False
            btnData.Enabled = False
            btnFWD.Enabled = False
            btnStop.Enabled = False
            btnRight.Enabled = False
            btnLeft.Enabled = False
            btnLight.Enabled = False
            btnSpeaker.Enabled = False
            btnCnct.Enabled = Not btnCnct.Enabled
            btnSensor.Enabled = False
            btnDcnct.Enabled = False
            ComboBox1.Enabled = True
        End If

    End Sub

    Private Sub btnFWD_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles btnFWD.MouseDown
        If SerialPort1.IsOpen Then
            SerialPort1.Write("U")
            data = False
        End If
    End Sub

    Private Sub btnStop_Click(sender As System.Object, e As System.EventArgs) Handles btnStop.Click
        If SerialPort1.IsOpen Then
            Try
                SerialPort1.Write("S")
                data = False
            Catch ex As Exception

            End Try
            
        End If
    End Sub

    Private Sub btnBWD_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles btnBWD.MouseDown
        If SerialPort1.IsOpen Then
            SerialPort1.Write("D")
            data = False
        End If
    End Sub

    Private Sub btnLeft_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles btnLeft.MouseDown
        If SerialPort1.IsOpen Then
            SerialPort1.Write("L")
            data = False
        End If
    End Sub

    Private Sub btnRight_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles btnRight.MouseDown
        If SerialPort1.IsOpen Then
            SerialPort1.Write("R")
            data = False
        End If
    End Sub

    Private Sub btnData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnData.Click
        If SerialPort1.IsOpen Then
            SerialPort1.Write("C")
            data = False
        End If
        Me.Focus()
    End Sub

    Private Sub btnSensor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSensor.Click
        If SerialPort1.IsOpen Then
            Try
                SerialPort1.Write("T")
                data = True
            Catch ex As Exception

            End Try
            Timer1.Start()
        End If
        Me.Focus()
    End Sub

    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        If data Then
            Reading = SerialPort1.ReadTo("x")
        End If
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
        lblReading.Text = Reading.Replace("x", "") + " cm"
    End Sub

    Private Sub btnBWD_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles btnBWD.MouseUp
        If SerialPort1.IsOpen Then
            SerialPort1.Write("S")
            data = False
        End If
    End Sub

    Private Sub btnFWD_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles btnFWD.MouseUp
        If SerialPort1.IsOpen Then
            SerialPort1.Write("S")
            data = False
        End If
    End Sub

    Private Sub btnLeft_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles btnLeft.MouseUp
        If SerialPort1.IsOpen Then
            SerialPort1.Write("S")
            data = False
        End If
    End Sub

    Private Sub btnRight_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles btnRight.MouseUp
        If SerialPort1.IsOpen Then
            SerialPort1.Write("S")
            data = False
        End If
    End Sub

    Private Sub btnLight_Click(sender As System.Object, e As System.EventArgs) Handles btnLight.Click
        imgLight = Not imgLight
        If SerialPort1.IsOpen Then
            If imgLight Then
                SerialPort1.Write("E")
                btnLight.BackgroundImage = My.Resources.LightOn
            ElseIf Not imgLight Then
                SerialPort1.Write("F")
                btnLight.BackgroundImage = My.Resources.LightOff
            End If

            data = False
        End If


    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles btnSpeaker.Click
        buzzer = Not buzzer
        If SerialPort1.IsOpen Then
            If buzzer Then
                SerialPort1.Write("V")
            ElseIf Not buzzer Then
                SerialPort1.Write("B")
            End If

            data = False
        End If
    End Sub

    Private Sub Form1_PreviewKeyDown(sender As Object, e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles Me.PreviewKeyDown
        inKeyPreview(e)
    End Sub

    Private Sub rdKeyboard_PreviewKeyDown(sender As Object, e As System.Windows.Forms.PreviewKeyDownEventArgs)
        inKeyPreview(e)
    End Sub

    Private Sub btnCnct_PreviewKeyDown(sender As Object, e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles btnCnct.PreviewKeyDown
        inKeyPreview(e)
    End Sub

    Private Sub btnData_PreviewKeyDown(sender As Object, e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles btnData.PreviewKeyDown
        inKeyPreview(e)
    End Sub

    Private Sub btnDcnct_PreviewKeyDown(sender As Object, e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles btnDcnct.PreviewKeyDown
        inKeyPreview(e)
    End Sub

    Private Sub btnFWD_PreviewKeyDown(sender As Object, e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles btnFWD.PreviewKeyDown
        inKeyPreview(e)
    End Sub

    Private Sub btnBWD_PreviewKeyDown(sender As Object, e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles btnBWD.PreviewKeyDown
        inKeyPreview(e)
    End Sub

    Private Sub btnLeft_PreviewKeyDown(sender As Object, e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles btnLeft.PreviewKeyDown
        inKeyPreview(e)
    End Sub

    Private Sub btnLight_PreviewKeyDown(sender As Object, e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles btnLight.PreviewKeyDown
        inKeyPreview(e)
    End Sub

    Private Sub btnRight_PreviewKeyDown(sender As Object, e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles btnRight.PreviewKeyDown
        inKeyPreview(e)
    End Sub

    Private Sub btnSensor_PreviewKeyDown(sender As Object, e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles btnSensor.PreviewKeyDown
        inKeyPreview(e)
    End Sub

    Private Sub btnSpeaker_PreviewKeyDown(sender As Object, e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles btnSpeaker.PreviewKeyDown
        inKeyPreview(e)
    End Sub

    Private Sub btnStop_PreviewKeyDown(sender As Object, e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles btnStop.PreviewKeyDown
        inKeyPreview(e)
    End Sub

    Private Sub inKeyPreview(e As System.Windows.Forms.PreviewKeyDownEventArgs)
        Select Case (e.KeyCode)
            Case Keys.Down, Keys.Up, Keys.Left, Keys.Right
                e.IsInputKey = True
        End Select
    End Sub
End Class
